package com.infosys.cabplanningdao.schedule.dao.impl;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import org.springframework.jdbc.core.support.JdbcDaoSupport;

import com.infosys.cabplanning.common.domain.Schedule;
import com.infosys.cabplanningdao.schedule.ScheduleDAO;

public class ScheduleDAOImpl extends JdbcDaoSupport implements ScheduleDAO {

	@Override
	public List<Schedule> getAllSchedule(Schedule scheduleSearchDTO) {

		String sql = "SELECT schedule_id, schedule_start_time, "
				+ "schedule_date FROM hackathon.schedule WHERE ";

		String queryWhereConditions = "";
		List<Object> parameterList = new ArrayList<Object>();
		if (scheduleSearchDTO.getScheduleId() != 0) {
			queryWhereConditions = " schedule_id = ? ";
			parameterList.add(scheduleSearchDTO.getScheduleId());
		}
		if (scheduleSearchDTO.getDate() != null) {
			queryWhereConditions = queryWhereConditions.isEmpty() ? queryWhereConditions
					: queryWhereConditions + " AND ";
			queryWhereConditions = queryWhereConditions + " schedule_date = ?";
			java.sql.Date sqlDate = new java.sql.Date(scheduleSearchDTO
					.getDate().getTimeInMillis());
			parameterList.add(sqlDate);
		}
		if (scheduleSearchDTO.getStartTime() != null
				&& !scheduleSearchDTO.getStartTime().isEmpty()) {
			queryWhereConditions = queryWhereConditions.isEmpty() ? queryWhereConditions
					: queryWhereConditions + " AND ";
			queryWhereConditions = queryWhereConditions + "  first_name = ? ";
			parameterList.add(scheduleSearchDTO.getStartTime());
		}
		
		if (!parameterList.isEmpty()) {
			System.out.println("queryWhereConditions : "+queryWhereConditions);
			sql = sql + queryWhereConditions;
			List<Schedule> scheduleList = getJdbcTemplate().query(sql,
					parameterList.toArray(), new ScheduleRowMapper());
			return scheduleList;
		} else {
			//TODO Throw
			return new ArrayList<Schedule>();
		}

		
	}

}
